<?php
class App_Module_Admin_Component_Structure_Tpl_List extends App_Module_Admin_Tpl_List
{
	protected $_showCells = array('title', 'icons');
	protected $_cellClasses = array('title' => 'big-title');
	protected $_emptyListMessage = 'NO_CATEGORIES';
	protected $_showEmptyList = true;
	protected $_sortable = true;

    public function GetList($cid)
    {
        Mozg::SubAction()->Add('RequiredJS', array($this, 'SubCategoriesJS'), array(), 'SubCategories');

        if (!$cid) $this->_showEmptyList = false;

        $this->cid = $cid;

		$this->isAllowDelete = Mozg::User()->CheckAccess('admin', 'structure', 'del');

		$rootElem = Mozg_Mapper::Get('Structure')->GetRoot();
		$this->rootId = $rootElem ? $rootElem->GetId() : 0;

        $icons = array(
        	array(
        	'add' => array(
        					'title' => 'ADD',
        					'url' => Mozg::Router('http')->Create('module=admin&ctrl=structure&action=add&cat=' .$cid)
        					),
        	)
        );

        $objects = Mozg_Mapper::Get('Structure')->GetList(array('parentId' => $cid));
        $content = $this->CreateList($objects);

        return $this->GetTpl('Base')->LoadTemplate($this->GetPathTitle($cid), $content, $icons);
    }

    protected function GetPathTitle($cid)
    {
    	$this->cid = $cid;
        return $this->render('structure/_path_title.phtml');
    }

    public function SubCategoriesJS()
    {
    	return $this->render('structure/js/_subcategories.js');
    }

    protected function GetHeaderTitle()
    {
    	return Mozg_Mapper::Get('Structure')->GetOne(array('id' => $this->cid))->GetName();
    }

    protected function GetHeaderIcons()
    {
    	$obj = Mozg_Mapper::Get('Structure')->GetOne(array('id' => $this->cid));
    	return $this->GetCellIcons($obj);
    }

	protected function GetCellTitle($obj)
	{
		$this->obj = $obj;
		return $this->render('structure/list/_title.phtml');
	}

	protected function GetCellIcons($obj)
	{
        $icons['edit'] = array(
        					'title' => 'EDIT',
        					'url' => Mozg::Router('http')->Create('module=admin&ctrl=structure&action=add&cat=' .$this->cid. '&subcat=' .$obj->GetId())
        					);

		$icons['html'] = array(
        					'title' => 'HTML',
        					'url' => Mozg::Router('http')->Create('module=admin&ctrl=structure&action=editbody&cat=' .$this->cid. '&subcat=' .$obj->GetId())
        					);

		if (($this->isAllowDelete && ($obj->GetId() != $this->rootId) && !$obj->GetBlocked()) || Mozg::User()->IsSuperUser())
		{
			$icons['delete'] = array(
	        					'title' => 'DELETE_CATEGORY?',
	        					'url' => Mozg::Router('http')->Create('module=admin&ctrl=structure&action=del&cat=' .$this->cid. '&subcat=' .$obj->GetId())
	        					);
		}
		else
		{
			$icons[] = array();
		}

		return $this->GetTpl('Icon')->GetIconSet($icons, 'list');
	}
}